package com.region.integral.common_user.consume.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.consume.dao.IUnsubscribeGoods;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.common_user.pojo.CUIEntity;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.UnsubscribeGoods;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.CommonUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iUnsubscribeGoods")
public class UnsubscribeGoodsImpl extends BaseHibernate implements IUnsubscribeGoods {

	@Override
	public void unsubscribeGoods(int userid, IntegralData integralData) throws IntegralException {
		CUIEntity cuiEntity = getCUIEntity(userid);
		if (cuiEntity != null) {
			int unsubscribeGoodsNum = cuiEntity.getUnsubscribeGoodsNum();
			
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<UnsubscribeGoods> unsubscribeGoods = regulation.getUnsubscribegoods();
			for (UnsubscribeGoods unGoods : unsubscribeGoods) {
				if (unGoods.getBelow()!=0 && unGoods.getBetween()==0&& unGoods.getBelow() >= unsubscribeGoodsNum ) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int ruleIntegral = unGoods.getIntegral();
					if(currIntegral >= Math.abs(ruleIntegral)){
						int totalIntegral = currIntegral+ ruleIntegral;
						cuiEntity.setCurrIntegral(totalIntegral);
						integralData.setSuccee(true);
						cuiEntity.setUnsubscribeGoodsNum(unsubscribeGoodsNum+1);
						hibernateTemplate.update(cuiEntity);
						hibernateTemplate.save(CommonUserLogUtil.log(userid, ruleIntegral, "unsubscribeGoods"));
					}
					else{
						integralData.setSuccee(false);
					}
					
				}
				if (unGoods.getBetween()!=0 &&unGoods.getBelow()< unsubscribeGoodsNum && unGoods.getBetween() >= unsubscribeGoodsNum) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int ruleIntegral = unGoods.getIntegral();
					if(currIntegral >= Math.abs(ruleIntegral)){
						int totalIntegral = currIntegral+ ruleIntegral;
						cuiEntity.setCurrIntegral(totalIntegral);
						integralData.setSuccee(true);
						cuiEntity.setUnsubscribeGoodsNum(unsubscribeGoodsNum+1);
						hibernateTemplate.update(cuiEntity);
						hibernateTemplate.save(CommonUserLogUtil.log(userid, ruleIntegral, "unsubscribeGoods"));
					}
					else{
						integralData.setSuccee(false);
					}
					
				}
				if (unGoods.getOver()!=0 && unGoods.getOver() < unsubscribeGoodsNum) {
					int currIntegral = cuiEntity.getCurrIntegral();
					int ruleIntegral = unGoods.getIntegral();
					if(currIntegral >= Math.abs(ruleIntegral)){
						int totalIntegral = currIntegral+ ruleIntegral;
						cuiEntity.setCurrIntegral(totalIntegral);
						integralData.setSuccee(true);
						integralData.setMessage(unGoods.getMessage());
						cuiEntity.setUnsubscribeGoodsNum(unsubscribeGoodsNum+1);
						hibernateTemplate.update(cuiEntity);
						hibernateTemplate.save(CommonUserLogUtil.log(userid, ruleIntegral, "unsubscribeGoods"));
					}
					else{
						integralData.setSuccee(false);
						integralData.setMessage("false");
					}
				}
			}
		}
	}

}
